// erik@codeborne.com

import org.junit.Test;

import static org.junit.Assert.*;

public class StringCalculatorTest {

	@Test
	public void emptyStringIsZero() {
		assertEquals(0, StringCalculator.add(""));
	}

	@Test
	public void singleValue() {
		assertEquals(1, StringCalculator.add("1"));
		assertEquals(23, StringCalculator.add("23"));
		assertEquals(4, StringCalculator.add("4"));
	}

	@Test
	public void twoValuesAreAdded() {
		assertEquals(2, StringCalculator.add("1,1"));
		assertEquals(23, StringCalculator.add("23,0"));
		assertEquals(10, StringCalculator.add("3,7"));
	}

	@Test
	public void anyNumberOfValuesAreAdded() {
		assertEquals(4, StringCalculator.add("1,1,1,1"));
		assertEquals(24, StringCalculator.add("23,0,1"));
		assertEquals(20, StringCalculator.add("3,7,7,3"));
	}

	@Test
	public void valuesCanBeOnSeparateLines() {
		assertEquals(4, StringCalculator.add("1,1\n1,1"));
		assertEquals(24, StringCalculator.add("23\n0\n1"));
		assertEquals(20, StringCalculator.add("3,7,7\n3"));
	}

	@Test
	public void firstLineCanSpecifyADifferentDelimiter() {
		assertEquals(4, StringCalculator.add("//;\n1;1\n1;1"));
		assertEquals(25, StringCalculator.add("// \n23\n0 1 1"));
		assertEquals(20, StringCalculator.add("//.\n3.7.7\n3"));
	}
	
	  @Test public void delimiterBeforeEmptyDataIsZero() { 
		  assertEquals(0,StringCalculator.add("//;\n")); 
		}
	  
	  @Test(expected = IllegalArgumentException.class) public void
	  negativeValuesNotAllowed() { 
		  assertEquals(4, StringCalculator.add("1,-1")); }
	 
	  @Test public void valuesLargerThan1000AreIgnored() { assertEquals(1002,
	  StringCalculator.add("1,1000,1001,1")); assertEquals(0,
	  StringCalculator.add("100000000,9782578")); }
	  
	  @Test public void negativeValueIsReportedInException() { try {
	  StringCalculator.add("1,-1"); fail("IllegalArgumentException expected");
	  } catch (IllegalArgumentException e) {
	  assertEquals("Negative values not allowed: -1", e.getMessage()); } }
	 
	  @Test public void allNegativeVhaluesAreReportedInException() { try {
	  StringCalculator.add("// \n1 -1 3 -5 -20");
	  fail("IllegalArgumentException expected"); } catch
	  (IllegalArgumentException e) {
	  assertEquals("Negative values not allowed: -1, -5, -20", e.getMessage());
	  } }
	  
	  @Test public void longerDelimitersAreSpecifiedInBrackets() {
	  assertEquals(129, StringCalculator.add("//[***]\n100***20***3\n6")); }
	 
}
